package com.yuan.java.commonIO;

import org.junit.Test;

import java.io.*;
import java.util.stream.Collectors;

/**
 * 测试JDK的WatchService监听文件变化
 *
 * @author yangzhilong
 */
public class TestWatchService {


    @Test
    public  void amain() throws IOException {
        String srcFilename = "D:\\1.txt";
        String charset = "GBK";
        InputStream fileInputStream = new FileInputStream(srcFilename);
        Reader fileReader = new InputStreamReader(fileInputStream, charset);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String singleLine;
        //读取到最后行,防止从头读取(加上这句代码只会打印出现在变动的内容,历史内容不打印)
        bufferedReader.lines().collect(Collectors.toList()).size();
        while (true) {
            if ((singleLine = bufferedReader.readLine()) != null) {
                if (!singleLine.trim().equals("")) {
                    System.out.println(singleLine);
                }
                continue;
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        bufferedReader.close();
    }
}